/**
 * @常用工具
 */

/**
 * @param {Object} options
 * icon | 图标
 * title | 标题
 */
let toast = function(options){
	uni.showToast({
		icon:options.icon || "success",
		title: options.title,
		duration:options.duration || 2000
	});
}

/**
 * @param {Object} title
 * title | 标题
 */
let loading = function(title){
	uni.showLoading({
		title,
		mask: false
	});
}

let hideLoading = function(){
	uni.hideLoading();
}

/**
 * 返回上一页
 */
let backTo = function(delta,duration=0){
	setTimeout(()=>{
		uni.navigateBack({
			delta
		})
	},duration)
}


/**
 * 进入某一页
 * url | String 页面路径
 * duration | Number 延迟返回 默认为0
 */
let toPage = function(url,duration=0){
	setTimeout(()=>{
		uni.navigateTo({
			url
		})
	},duration)
}

/**
 * 进入tabbar页面
 */
let toSwitchTab = function(url,duration=0){
	setTimeout(()=>{
		uni.switchTab({
			url
		})
	},duration)
}

/**
 * 关闭当前打开的所有页面进入某个页面
 */
let toReLaunch = function(url,duration=0){
	setTimeout(()=>{
		uni.reLaunch({
			url
		})
	},duration)
}

/**
 * 关闭当前页面 进入某个页面
 */
let redirectTo = function(url,duration=0){
	setTimeout(()=>{
		uni.redirectTo({
			url
		})
	},duration)
}

// 打乱数组
let shuffle = function(arr) {
	for (let i = 1; i < arr.length; i++) {
		const random = Math.floor(Math.random() * (i + 1));
		[arr[i], arr[random]] = [arr[random], arr[i]];
	}
	return arr;
}

// 比较两个对象是否一样(key:value 一样)
let isObjectEquality = function(...args){
	if(args.length <= 0){
		return {
			msg:"请传入比较的对象"
		}
	}
	
	let obj1 = Object.keys(args[0]);
	let obj2 = Object.keys(args[1]);
	
	if(obj1.length !== obj2.length){
		return {
			msg:"两个对象长度不同"
		}
	}
	
	// 验证key是否相等
	let keyIndex = 0;
	obj1.forEach((val1) => {
		obj2.forEach((val2) => {
			if(val1 === val2){
				keyIndex += 1;
			}
		})
	})
	if(obj1.length !== keyIndex){
		return {
			msg:"两个对象的key不同"
		}
	}
	
	let index = 0; // 记录相同属性值的个数
	obj1.forEach(key => {
		if (args[0][key] === args[1][key]){
			index += 1;
		}
	})
	if(obj1.length === index){
		return true;
	}
}


export {
	toast,
	loading,
	hideLoading,
	toPage,
	backTo,
	shuffle,
	isObjectEquality,
	toSwitchTab,
	toReLaunch,
	redirectTo
}